/*
 *所有关于Mechanica类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*MechanicaService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Demo.IRepositories;

namespace VOL.Demo.Services
{
    public partial class MechanicaService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IMechanicaRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public MechanicaService(
            IMechanicaRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }


        public override PageGridData<Mechanica> GetPageData(PageDataOptions options)
        {
            QueryRelativeExpression = (IQueryable<Mechanica> queryable) =>
            {

                if (options.Value != null)
                {
                    switch (options.Value.GetInt())
                    {
                        case 1:
                            queryable = queryable = queryable.Where(c => c.sbxh == "锻造设备");
                            break;
                        default:
                            break;

                    }
                }
                //if (options.Value.GetInt() == 1)
                //{
                //    queryable = queryable.Where(c => c.sbxh == "锻造设备");
                //}else if(true){

                //}
                return queryable;
            };

            //查询table界面显示求和
            SummaryExpress = (IQueryable<Mechanica> queryable) =>
            {
                return queryable.GroupBy(x => 1).Select(x => new
                {
                    //AvgPrice注意大小写和数据库字段大小写一样
                    sl = x.Sum(o => o.sl).ToString("f2")
                })
                .FirstOrDefault();
            };
            return base.GetPageData(options);
        }


        WebResponseContent webResponse = new WebResponseContent();
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            //saveDataModel.MainData["sbbm"] = "111";
            
            AddOnExecuting = (Mechanica mechanica, object list) =>
            {

                //SerialNumberGenerator.ResetSerialNumber(1000);
                string serial3 = SerialNumberGenerator.GetNextSerialNumber("ORD", 8, "yyyyMMdd");
                mechanica.sbbm = serial3;
                return webResponse.OK();
            };
            return base.Add(saveDataModel);
        }
        public override WebResponseContent Update(SaveModel saveModel)
        {
            
            return base.Update(saveModel);
        }

        public class SerialNumberGenerator
        {
            private static int _currentNumber = 0;
            private static readonly object _lockObj = new object();

            /// <summary>
            /// 获取下一个自增单号
            /// </summary>
            /// <param name="prefix">单号前缀(可选)</param>
            /// <param name="digits">数字位数(不足时前面补零)</param>
            /// <param name="dateFormat">日期格式(可选，如"yyyyMMdd")</param>
            /// <returns>格式化后的单号</returns>
            public static string GetNextSerialNumber(string prefix = "", int digits = 6, string dateFormat = null)
            {
                lock (_lockObj)
                {
                    _currentNumber++;

                    // 获取当前日期部分(如果有)
                    string datePart = string.IsNullOrEmpty(dateFormat)
                        ? ""
                        : DateTime.Now.ToString(dateFormat);

                    // 格式化数字部分
                    string numberPart = _currentNumber.ToString().PadLeft(digits, '0');

                    // 组合各部分
                    return $"{prefix}{datePart}{numberPart}";
                }
            }

            /// <summary>
            /// 重置序列号(可选)
            /// </summary>
            /// <param name="newStartNumber">新的起始数字</param>
            public static void ResetSerialNumber(int newStartNumber = 0)
            {
                lock (_lockObj)
                {
                    _currentNumber = newStartNumber;
                }
            }
        }
    }


}
